/**
 * 
 */
package dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import model.MonHocM;
import model.MonHocNhomTuChonM;
import model.NhomMonTuChonM;
import util.DAL;
import util.MyConnectUnit;

public class MonHocNhomTuChonD {
	private MyConnectUnit conn;

	/**
	 * @throws Exception 
	 * 
	 */
	public MonHocNhomTuChonD() throws Exception {
		super();
		this.conn = DAL.getDAL();
	}

	/**
	 * @function get all MonHocNhomTuChon
	 * @param from
	 * @param to
	 * @return
	 * @throws Exception
	 */
	public ArrayList<MonHocNhomTuChonM> getAlls(String ten, int from, int to) throws Exception {
		String limit = from + ", " + to;
		ArrayList<MonHocNhomTuChonM> ls = new ArrayList<MonHocNhomTuChonM>();
		ResultSet rs = this.conn.Selects(
						"mhntc.id_mon_hoc, mh.ten, mhntc.id_nhom_tu_chon, nmtc.ten",
						"MON_HOC_NHOM_TU_CHON mhntc, MON_HOC mh, NHOM_MON_TU_CHON nmtc",
						"mhntc.id_mon_hoc = mh.id_mon_hoc AND mhntc.id_nhom_tu_chon = nmtc.id_nhom_mon_tu_chon AND mh.ten LIKE '%" + ten + "'",
						"mhntc.id_mon_hoc ASC",
						limit);
		MonHocNhomTuChonM l = null;
		while (rs.next()) {
			l = new MonHocNhomTuChonM();
			
			l.setId_mon_hoc(rs.getInt("mhntc.id_mon_hoc"));
			MonHocM mh = new MonHocM();
			mh.setTen(rs.getString("mh.ten"));
			l.setTen_mon_hoc(mh);
			
			l.setId_nhom_tu_chon(rs.getInt("mhntc.id_nhom_tu_chon"));
			NhomMonTuChonM nmtc = new NhomMonTuChonM();
			nmtc.setTen(rs.getString("nmtc.ten"));
			l.setTen_nhom_mon_tu_chon(nmtc);
			ls.add(l);
		}
		return ls;
	}
	
	/**
	 * @function get all MonHocNhomTuChon
	 * @param Condition
	 * @param Limit
	 * @param OrderBy
	 * @return
	 * @throws Exception
	 */
	public ArrayList<MonHocNhomTuChonM> getAll(String Condition, String Limit, String OrderBy) throws Exception {
		ArrayList<MonHocNhomTuChonM> ls = new ArrayList<MonHocNhomTuChonM>();
		ResultSet rs = this.conn.Select("MON_HOC_NHOM_TU_CHON", Condition, Limit, OrderBy);
		MonHocNhomTuChonM l = null;
		while (rs.next()) {
			l = new MonHocNhomTuChonM();
			l.setId_mon_hoc(rs.getInt("id_mon_hoc"));
			l.setId_nhom_tu_chon(rs.getInt("id_nhom_tu_chon"));
			ls.add(l);
		}
		return ls;
	}

	/**
	 * @function get all MonHocNhomTuChon with Condition + Limit
	 * @param Condition
	 * @param Limit
	 * @return
	 * @throws Exception
	 */
	public ArrayList<MonHocNhomTuChonM> getAll(String Condition, String Limit) throws Exception {
		return getAll(Condition, Limit, null);
	}

	/**
	 * @function get all MonHocNhomTuChon with Condition
	 * @return
	 * @throws Exception
	 */
	public ArrayList<MonHocNhomTuChonM> getAll(String Condition) throws Exception {
		return getAll(Condition, null);
	}
	
	/**
	 * @function get all MonHocNhomTuChon no parameter
	 * @return
	 * @throws Exception
	 */
	public ArrayList<MonHocNhomTuChonM> getAll() throws Exception {
		return getAll(null);
	}
	
	/**
	 * @function search ten MonHocNhomTuChon
	 * @param ten
	 * @return
	 * @throws Exception
	 */
	public ArrayList<MonHocNhomTuChonM> searchMHNTC(int id) throws Exception {
		ArrayList<MonHocNhomTuChonM> list = getAll("id_mon_hoc = " + id);
		return list;
	}
	
	/**
	 * @function check exists MonHocNhomTuChon
	 * @param id_mon_hoc
	 * @param id_nhom_tu_chon
	 * @return
	 * @throws Exception
	 */
	public int checkExists(int id_mon_hoc, int id_nhom_tu_chon) throws Exception {
		ArrayList<MonHocNhomTuChonM> list = getAll("id_mon_hoc = " + id_mon_hoc + " AND id_nhom_tu_chon = " + id_nhom_tu_chon);
		return list.size();
	}
	
	/**
	 * @function get all MonHocNhomTuChon with search + paging
	 * @param from
	 * @param to
	 * @return
	 * @throws Exception
	 */
	public ArrayList<MonHocNhomTuChonM> getLimit(int id, int from, int to) throws Exception {
		String limit = from + ", " + to;
		ArrayList<MonHocNhomTuChonM> list = getAll("id_mon_hoc = " + id, limit);
		return list;
	}

	/**
	 * function insert MonHocNhomTuChon
	 * @param mhntc
	 * @return
	 * @throws Exception
	 */
	public boolean Insert(MonHocNhomTuChonM mhntc) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("id_mon_hoc", mhntc.getId_mon_hoc());
		map.put("id_nhom_tu_chon", mhntc.getId_nhom_tu_chon());
		return this.conn.Insert("MON_HOC_NHOM_TU_CHON", map);
	}

	/**
	 * @function delete MonHocNhomTuChon
	 * @param id_mon_hoc
	 * @param id_nhom_tu_chon
	 * @return
	 * @throws Exception
	 */
	public boolean Delete(int id_mon_hoc, int id_nhom_tu_chon) throws Exception {
		return this.conn.Delete("MON_HOC_NHOM_TU_CHON", "id_mon_hoc = " + id_mon_hoc + " AND id_nhom_tu_chon = " + id_nhom_tu_chon);
	}
}
